using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;
using VitalShining.WebControls;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;


namespace CARMS.Admin
{
    [Function("Admin.ConditionControl")]
    public partial class ConditionList :PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageQueryAdapter1.PerformQuery();
            }
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {

            var collection = new ColumnToActionCollection();
            e.RecordCount = collection.GetDbCount((IPredicate)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var searchExpression = new PredicateExpression();
            //if (txtReferenceNo.Text.Trim().Length > 0)
            //{
            //    searchExpression.AddWithAnd(ColumnToActionFields.RegistrationNumber % ("%" + txtReferenceNo.Text.Trim() + "%"));

            //}
            //if (txtClientName.Text.Trim().Length > 0)
            //{
            //    //step 1 select client
            //    var collection = new CustomerCollection();
            //    collection.GetMulti(CustomerFields.CustomerName % ("%" + txtClientName.Text.Trim() + "%"));

            //    //step 2 select ColumnToAction id
            //    var clientIDList = from item in collection select item.CustomerId;
            //    var clientToColumnToActionCollection = new ClientToColumnToActionCollection();
            //    clientToColumnToActionCollection.GetMulti(new FieldCompareRangePredicate(ClientToColumnToActionFields.ClientId, false, clientIDList.ToArray()));

            //    //step 3 select ColumnToAction table
            //    var ColumnToActionidList = from clientToColumnToAction in clientToColumnToActionCollection select clientToColumnToAction.ColumnToActionId;
            //    searchExpression.AddWithAnd(new FieldCompareRangePredicate(ColumnToActionFields.ColumnToActionId, false, ColumnToActionidList.ToArray()));
            //}



            //if (dplAnalysisType.SelectedValue.Trim().Length > 0)
            //{
            //    searchExpression.AddWithAnd(ColumnToActionFields.AnalysysType == new Guid(dplAnalysisType.SelectedValue));
            //}

            //if (dplColumnToActionStatus.SelectedValue.Trim().Length > 0)
            //{
            //    searchExpression.AddWithAnd(ColumnToActionFields.Status == new Guid(dplColumnToActionStatus.SelectedValue));
            //}
            //if (NoticeColumnToActionID != Guid.Empty)
            //{
            //    searchExpression.Add(ColumnToActionFields.ColumnToActionId == NoticeColumnToActionID);
            //}

            //searchExpression.AddWithAnd(ColumnToActionFields.IsActive == true);
            e.Condition = searchExpression;
        }


        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridList.SortExpression)
            {
                case "ActionID":
                    sort = new SortExpression(ColumnToActionFields.ActionId | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "TableName":
                    sort = new SortExpression(ColumnToActionFields.TableName | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "ColumnName":
                    sort = new SortExpression(ColumnToActionFields.ColumnName | (SortOperator)(int)gridList.SortDirection);
                    break;

                default:
                    sort = new SortExpression(ColumnToActionFields.ActionId | SortOperator.Descending);
                    break;
            }


            var collection = new ColumnToActionCollection();
            collection.GetMulti((IPredicate)e.Condition, 0,
                                sort,
                                null,
                                e.PageIndex + 1, e.PageSize);
            e.DataSource = collection;
        }


        protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (ColumnToActionEntity)e.Row.DataItem;

                //var lblPageName = e.Row.FindControl("lblPageName") as Label;
       


                //if (entity.ColumnToActionId == InputColumnToActionID)
                //{
                //    e.Row.Attributes.Add("style", "color:red;");
                //}
                //e.Row.Attributes.Add("onDblClick", "OpenColumnToActionView('" + entity.ColumnToActionId + "')");

                //var lblClientName = e.Row.FindControl("lblClientName") as Label;
                //var client = ColumnToActionBusiness.GetClientCustomerEntityByColumnToActionID(entity.ColumnToActionId);
                //if (client != null)
                //{
                //    lblClientName.Text = client.CustomerName;
                //    lblClientName.ToolTip = lblClientName.Text;
                //}
            }
        }

        protected void gridList_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                //case "ModifyItem":
                //    var ColumnToActionID = new Guid(e.CommandArgument.ToString());
                //    var ColumnToActionEntity = new ColumnToActionEntity(ColumnToActionID);
                //    if (ColumnToActionEntity.ColumnToActionType == DictionaryCategoryConst.ColumnToActionType.ConventionColumnToAction && ColumnToActionEntity.Status == DictionaryCategoryConst.ColumnToActionStatus.InPreparation)
                //    {
                //        RediretResolveUrl(SiteUrls.ColumnToActionInfo.ColumnToActionSimpleModify + "?ColumnToActionID=" + ColumnToActionEntity.ColumnToActionId + "&RtUrl=" + RequestUrl);
                //    }
                //    else
                //    {
                //        RediretResolveUrl(SiteUrls.ColumnToActionInfo.ColumnToActionModify + "?ColumnToActionID=" + ColumnToActionEntity.ColumnToActionId + "&RtUrl=" + RequestUrl);
                //    }
                //    break;
                //case "DeleteItem":
                //    var boolSuccess = ColumnToActionBusiness.DeleteColumnToAction(new Guid(e.CommandArgument.ToString()));
                //    if (boolSuccess)
                //    {
                //        PageQueryAdapter1.PerformQuery();
                //    }
                //    else
                //    {
                //        base.Alert("Delete Failed.");
                //    }
                //    break;
            }
        }

        protected void btnRefresh_Click(object sender,EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }
    }
}
